//package com.yehun.utils;
//
//import javax.mail.Message;
//import javax.mail.MessagingException;
//import javax.mail.Session;
//import javax.mail.Transport;
//import javax.mail.internet.InternetAddress;
//import javax.mail.internet.MimeMessage;
//import javax.mail.internet.MimeUtility;
//import java.io.UnsupportedEncodingException;
//import java.util.Date;
//import java.util.Objects;
//import java.util.Properties;
//
///**
// * 邮件发送
// * Created by Genius on 2017/5/16 0016.
// */
//public class EmailUtils {
//
//    public static void logException(Exception e) {
//        String mailTitle = "异常";
//        String mailContent = ExceptionUtils.getExceptionAllinformation(e);
//        sendMail(mailTitle, mailContent);
//    }
//
//    public static void sendMail(String mailTitle, String mailContent) {
//        String mailName = PropertyUtils.getConfig(Constant.Properties.Key.Config.SmtpMailName);
//        sendMail(mailName, mailTitle, mailContent);
//    }
//
//    public static void sendMail(String mailName, String mailTitle, String mailContent) {
//        String mailStr = PropertyUtils.getConfig(Constant.Properties.Key.Config.NotifyMailList);
//        String[] toMails = null;
//        if (mailStr != null) {
//            toMails = mailStr.split("|");
//        }
//        if (toMails == null) {
//            return;
//        }
//        sendMail(toMails, mailName, mailTitle, mailContent);
//    }
//
//    public static void sendMail(String[] toMails, String mailName, String mailTitle, String mailContent) {
//        String smtpMail = PropertyUtils.getConfig(Constant.Properties.Key.Config.SmtpMailAddress);
//        String smtpUser = PropertyUtils.getConfig(Constant.Properties.Key.Config.SmtpMailUser);
//        String smtpPass = PropertyUtils.getConfig(Constant.Properties.Key.Config.SmtpMailPassword);
//        String smtpHost = PropertyUtils.getConfig(Constant.Properties.Key.Config.SmtpMailHost);
//        sendMail(smtpMail, smtpUser, smtpPass, smtpHost, toMails, mailName, mailTitle, mailContent);
//    }
//
//    public static void sendMail(String fromMail, String user, String password, String smtpHost,
//                                String[] toMails,
//                                String mailName,
//                                String mailTitle,
//                                String mailContent) {
//        Properties props = new Properties();
//        props.put("mail.smtp.host", smtpHost);//存储发送邮件服务器的信息
//        props.put("mail.smtp.auth", "true");//同时通过验证
//        props.put("mail.transport.protocol", "smtp");
//        Transport transport = null;
//        try {
//            Session session = Session.getDefaultInstance(props, null); //新建一个邮件会话
//            //session.setDebug(true); //打印一些调试信息。
//            MimeMessage message = new MimeMessage(session);//新建一个消息对象
//            //设置发件人的昵称和地址
//            message.setFrom(new InternetAddress((mailName == null || Objects.equals(mailName, "")) ? fromMail : String.format("%s<%s>", MimeUtility.encodeText(mailName), fromMail)));
//            InternetAddress[] addresses = null;
//            if (toMails != null && toMails.length > 0) {
//                addresses = new InternetAddress[toMails.length];
//                for (int i = 0; i < toMails.length; i++) {
//                    addresses[i] = new InternetAddress(toMails[i]);
//                }
//            }
//            if (addresses == null) {
//                return;
//            }
//            message.setRecipients(Message.RecipientType.TO, addresses);//设置收件人,并设置其接收类型为TO
//            message.setSubject(mailTitle);//设置标题
//            message.setContent(mailContent, "text/html;charset=gbk");
//            message.setSentDate(new Date());//设置发信时间
//            message.saveChanges();//存储邮件信息
//            //发送邮件
//            //transport = session.getTransport("smtp");
//            transport = session.getTransport();
//            transport.connect(user, password);
//            transport.sendMessage(message, message.getAllRecipients());//发送邮件,其中第二个参数是所有已设好的收件人地址
//        } catch (MessagingException | UnsupportedEncodingException e) {
//            e.printStackTrace();
//        } finally {
//            if (transport != null) {
//                try {
//                    transport.close();
//                } catch (MessagingException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//    }
//
//
//    public static void main(String[] args) throws Exception {
//        String fromMail = "pythonhk@sina.com";
//        String password = "genius1225";
//        String smtpHost = "smtp.sina.com.cn";
//        String toMail = "yehunhk@163.com";
//
//        //sendMail(toMail, "123", "testTitle", "testContent");
//        //sendMail(fromMail, fromMail, password, smtpHost, toMail, "testTitle", "testContent");
//    }
//
//}
